package com.ruoyi.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysOrder;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.service.ISysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysOrderServiceImpl  extends ServiceImpl<SysOrderMapper, SysOrder>implements ISysOrderService {


    @Autowired
    private SysOrderMapper sysOrderMapper;
    @Override
    public void batchSave(List<SysOrder> orderList) {

        if (orderList != null && !orderList.isEmpty()) {
            saveBatch(orderList);
        }



    }

    /**
     * 检查 recordNo 是否已经存在
     * @param recordNo 订单的唯一标识
     * @return 如果存在，返回 true；否则返回 false
     */
    @Override
    public boolean checkRecordNoExist(String recordNo) {
        // 使用 MyBatis-Plus 的 QueryWrapper 来查询数据库
        QueryWrapper<SysOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_no", recordNo);  // 设置查询条件，检查 record_no 字段是否与传入的值相等
        int count = sysOrderMapper.selectCount(queryWrapper);  // 查询符合条件的记录数
        return count > 0;  // 如果记录数大于 0，说明 recordNo 存在
    }



    @Override
    public Long queryTotal() {
      Long num  =sysOrderMapper.queryTotal();
        return num;
    }

    @Override
    public Long resultEqualMarkType() {
        Long equal  =sysOrderMapper.resultEqualMarkType();
        return equal;
    }

    @Override
    public void updateBatchByRecordNos(Map<String, Long> recordResultMap) {
        for (Map.Entry<String, Long> entry : recordResultMap.entrySet()) {
            this.update(
                    new SysOrder(),
                    new LambdaUpdateWrapper<SysOrder>()
                            .eq(SysOrder::getRecordNo, entry.getKey())
                            .set(SysOrder::getResult, entry.getValue())
            );
        }
    }


}
